<?php
/**
 * Created by PhpStorm.
 * User: yaoihu
 * Date: 2019/4/22
 * Time: 20:24
 */

namespace Develop\Http\Controllers;


use App\Helpers\SelfResponse;
use App\Http\Controllers\Controller;
use Develop\Models\CurrencyModel;
use Illuminate\Http\Request;

class CurrencyController extends Controller
{
    public function currencies()
    {
        $list = CurrencyModel::get();

        return view('develop::currency.list',[
            'list'=>$list,
            'page_title'=>'币种管理',
        ]);
    }

    public function create(Request $request)
    {
        if($request->isMethod('post')){
            $currencyModel = new CurrencyModel();

            if($currencyModel->addCurrency($currencyModel,$request)){
                return SelfResponse::success('添加成功','d_Currency_currencies');
            }

            return SelfResponse::error('添加失败!');
        }else{

            return view('develop::currency.add_or_edit',[
                'page_title'=>'添加币种'
            ]);
        }
    }

    public function update(Request $request, $id)
    {

        if($request->isMethod('post')){

            $currencyModel = new CurrencyModel();
            if($currencyModel->editCurrency($request,$id)){
                return SelfResponse::success('编辑成功','d_Currency_currencies');
            }

            return SelfResponse::error('编辑失败!');
        }else{
            $info = CurrencyModel::find($id);
            return view('develop::currency.add_or_edit',[
                'page_title'=>'编辑币种',
                'info'=>$info
            ]);
        }

    }

    public function delete($id)
    {
        $currencyModel = new CurrencyModel();
        if($currencyModel->deleteCurrency($id)){
            return SelfResponse::success('删除成功','d_Currency_currencies');
        }
        return SelfResponse::error('删除失败!');
    }

    public function onOff($id)
    {
        $info = CurrencyModel::find($id);
        if(!$info){
            return SelfResponse::success('币种不存在');
        }
        $info->status = abs($info->status-1);
        if(!$info->save()){
            return SelfResponse::error('失败');
        }
        return SelfResponse::success('成功','d_Currency_currencies');
    }
}